﻿using Newtonsoft.Json;
using System;
using System.Security.Principal;

namespace WebFormsIdentity.Models
{
    /// <summary>
    /// 用户信息
    /// </summary>
    public class UserInfo : IPrincipal
    {
        private int userId;
        /// <summary>
        /// 用户ID
        /// </summary>
        public int UserId
        {
            get { return userId; }
            set { userId = value; }
        }

        private int roleId;
        /// <summary>
        /// 角色ID
        /// </summary>
        public int RoleId
        {
            get { return roleId; }
            set { roleId = value; }
        }

        private string userName = null;
        /// <summary>
        /// 用户真实姓名
        /// </summary>
        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }

        private string mobilePhone = "";
        /// <summary>
        /// 手机号码
        /// </summary>
        public string MobilePhone
        {
            get { return mobilePhone; }
            set
            {
                if (value != null)
                {
                    mobilePhone = value;
                }
            }
        }

        // 如果还有其它的用户信息，可以继续添加。

        public override string ToString()
        {
            return string.Format("UserId: {0}, RoleId: {1}, UserName: {2}, IsAdmin: {3}, 手机号码: {4}", userId, roleId, userName, IsInRole("Admin"), mobilePhone);
        }

        #region IPrincipal Members

        [JsonIgnore]
        public IIdentity Identity
        {
            get { throw new NotImplementedException(); }
        }

        public bool IsInRole(string role)
        {
            if (string.Compare(role, "Admin", true) == 0)
            {
                return RoleId == 1;
            }
            else
            {
                return RoleId > 0;
            }
        }

        #endregion
    }
}